<template>
    <div class="app-container">
        <el-row :gutter="20" class="header">
            <el-col :span="7">
                <el-input placeholder="搜索角色名" v-model="queryForm.query" clearable></el-input>
            </el-col>
            <el-button type="primary" :icon="Search" @click="initRoleList">搜索</el-button>
            <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue()">新增</el-button>
            <el-popconfirm title="你确定删除选中的这些数据吗？" @confirm="handleDelete()">
                <template #reference>
                    <el-button type="danger" :disabled="delBtnStatus" :icon="Delete">批量删除</el-button>
                </template>
            </el-popconfirm>
        </el-row>
        <el-table :data="tableData" stripe style="width: 100%" @selection-change="handlerSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="name" label="角色名" width="100" align="center" />
            <el-table-column prop="code" label="权限字符" width="200" align="center" />
            <el-table-column prop="createTime" label="创建时间" width="200" align="center" />
            <el-table-column prop="remark" label="备注" />
            <el-table-column prop="action" label="操作" width="400" fixed="right" align="center">
                <template v-slot="scope">
                    <el-button type="primary" :icon="Tools"
                        @click="handleMenuDialogValue(scope.row)">分配权限</el-button>
                    <el-button v-if="scope.row.code != 'admin'" type="primary" :icon="Edit"
                        @click="handleDialogValue(scope.row.id)" />
                    <el-popconfirm v-if="scope.row.code != 'admin'" title="您确定要删除这条记录吗？"
                        @confirm="handleDelete(scope.row.id)">
                        <template #reference>
                            <el-button type="danger" :icon="Delete" />
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination v-model:current-page="queryForm.pageNum" v-model:page-size="queryForm.pageSize"
            :page-sizes="[5, 10, 20, 40]" layout="total, sizes, prev, pager, next, jumper" :total="total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    <Dialogs v-model="dialogVisible" :id="id" :dialogTitle="dialogTitle" @initRoleList="initRoleList" />
    <MenuDialogs v-model="menuDialogVisible" :id="id" :dialogTitle="dialogTitle" @initRoleList="initRoleList" />
</template>

<script setup>
import { ref } from 'vue';
import requestUitl, { getServerUrl } from '@/util/request';
import { Search, Delete, DocumentAdd, Edit, Tools, RefreshRight } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import Dialogs from './components/dialog'
import MenuDialogs from './components/menuDialog'
// 数据列表
const tableData = ref([]);
// 总条数
const total = ref(0);
// 查询用
const queryForm = ref({
    query: '',
    pageNum: 1,
    pageSize: 5
});
// 模态框状态
const dialogVisible = ref(false);
// 模态框标题
const dialogTitle = ref("");
// 模态框打开时选中的id
const id = ref(-1);
// 选中的行
const multipleSelection = ref([]);
// 已有的角色列表
const roleList = ref([])
// 分配角色弹窗的开关
const menuDialogVisible = ref(false)
// 删除按钮是否可用状态
const delBtnStatus = ref(true);
// 表格勾选事件
const handlerSelectionChange = (selection) => {
    multipleSelection.value = selection;
    delBtnStatus.value = selection.length == 0;
}
// 打开模态框之前的操作
const handleDialogValue = (roleId) => {
    if (roleId) {
        id.value = roleId;
        dialogTitle.value = "角色修改"
    } else {
        id.value = -1;
        dialogTitle.value = "角色添加"
    }
    dialogVisible.value = true
}

const handleMenuDialogValue = (role) => {
    //.id, scope.row.roleList
    id.value = role.id;
    roleList.value = role.roleList;
    dialogTitle.value = role.name;
    menuDialogVisible.value = true;
}

// 获取数据列表
const initRoleList = async () => {
    const request = await requestUitl.get("/sys/role/list", queryForm.value);
    let data = request.data;
    tableData.value = data.result.roleList;
    total.value = data.result.total;
}

const handleDelete = async (id) => {
    let ids = [];
    if (id) {
        ids.push(id);
    } else {
        multipleSelection.value.forEach(i => ids.push(i.id))
    }
    const result = await requestUitl.post('/sys/role/delete', ids);
    let data = result.data;
    if (data.code == 2000) {
        ElMessage({
            type: 'success',
            message: '执行成功！'
        });
        initRoleList();
    } else {
        ElMessage({
            type: 'error',
            message: data.message
        });
    }
}

// 重置密码
const handleResetPassword = async (id) => {
    const result = await requestUitl.get('/sys/role/resetPassword/' + id)
    let data = result.data;
    if (data.code == 2000) {
        ElMessage({
            type: 'success',
            message: '重置成功！'
        });
    } else {
        ElMessage({
            type: 'error',
            message: data.message
        });
    }
}

const statusChangeHandle = async (row) => {
    const result = await requestUitl.get(`/sys/role/updateStatus/${row.id}/${row.status}`);
    let data = result.data;
    if (data.code == 2000) {
        ElMessage({
            type: 'success',
            message: '修改成功！'
        });
    } else {
        ElMessage({
            type: 'error',
            message: data.message
        });
    }
}

initRoleList();
// 每页显示的数据条数改变时调用
const handleSizeChange = (pageSize) => {
    queryForm.value.pageSize = pageSize;
    queryForm.value.pageNum = 1;
    initRoleList();
}
// 切换页数时调用
const handleCurrentChange = (pageNum) => {
    queryForm.value.pageNum = pageNum;
    initRoleList();
}

</script>

<style lang="scss" scoped>
.header {
    padding-bottom: 16px;
    box-sizing: border-box;
}

.el-pagination {
    float: right;
    padding: 20px;
    box-sizing: border-box;
}

::v-deep th.el-table__cell {
    word-break: break-word;
    background-color: #f8f8f9 !important;
    color: #515a6e;
    height: 40px;
    font-size: 13px;
}
</style>